<?php

/*
 * Author: Quan Van Sinh
 * Email: sinhvnb@vietnambiz.com
 */

class detail {

    function detail(){}

    function viewArtitle() {
        global $db, $config;
        $db->query("UPDATE article SET view_count=view_count+1 WHERE link='" . $config['link'] . "'");
        $sql = "SELECT a.id,a.cat_id,c.name as category_name,c.link as cat_link,a.title,a.image,a.short_description,a.description,a.create_date,a.comment,a.show_comment,a.thumb_up FROM article a LEFT JOIN article_category c ON a.cat_id=c.id WHERE 1 AND a.status='yes' AND a.link='" . $config['link'] . "'";
        $rsArticle = $db->query_first($sql);
        $config['link'] = $rsArticle['cat_link'];
        if ($rsArticle) {
            return $rsArticle;
        } else {
            header("Location: " . $config['domain']);
        }
    }

    function viewAttachFiles() {
        global $db, $config;
        $rsId = $db->query_first("SELECT id FROM article WHERE link='" . $config['link'] . "'");
        if ($rsId) {
            $rsAttachFiles = $db->query("SELECT id,real_filename,filename FROM article_attach WHERE article_id=" . $rsId['id']);
            return $rsAttachFiles;
        }
    }

    function getTagByArticleId($id) {
        global $db, $config;
        $rsTagsId = $db->query("SELECT tag_id FROM article_tags WHERE article_id=" . intval($id));
        $totalTagsId = $db->num_rows($rsTagsId);
        $e = 0;
        $strTagsId = '';
        while ($rs = $db->fetch_array($rsTagsId)) {
            $e++;
            if ($e == $totalTagsId) {
                $strTagsId .= $rs['tag_id'];
            } else {
                $strTagsId .= $rs['tag_id'] . ',';
            }
        }
        if ($strTagsId != '') {
            $rsTags = $db->query("SELECT tags,tags_link FROM tags WHERE id IN ($strTagsId)");
            return $rsTags;
        }
    }

    function get_cat_id_to_arr($parent_id = 0) {
        global $db;
        if (!$trees)
            $trees = array();
        $rsCat = $db->query("SELECT * FROM article_category WHERE  parent_id = " . intval($parent_id));
        if ($rsCat) {
            while ($rs = $db->fetch_array($rsCat)) {
                $trees[] = $rs['id'];
            }
        }
        return $trees;
    }

    function getLastestArticlesByCategoryId($categoryId) {
        global $db;
        $arrCatId = $this->get_cat_id_to_arr($categoryId);
        $str_cat_id = '';
        for ($i = 0; $i < count($arrCatId); $i++) {
            if ($i == (count($arrCatId) - 1)) {
                $str_cat_id .= $arrCatId[$i];
            } else {
                $str_cat_id .= $arrCatId[$i] . ',';
            }
        }
        if ($str_cat_id == '') {
            $sql = "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE a.cat_id=" . intval($categoryId) . " AND a.status='yes' ORDER BY a.create_date DESC";
        } else {
            $sql = "SELECT a.title,a.image,a.link,a.short_description,c.name as cat_name, c.link as cat_link FROM article a LEFT JOIN article_category c
            ON a.cat_id = c.id WHERE a.cat_id=" . intval($categoryId) . " OR a.cat_id IN ($str_cat_id) AND a.status='yes' ORDER BY a.create_date DESC";
        }

        $rs = $db->query_first($sql);
        return $rs;
    }

    function getArticleByCategoryIdOffsetLimit($categoryId, $offset=0, $limit=10) {
        global $db;
        $arrCatId = $this->get_cat_id_to_arr($categoryId);
        $str_cat_id = '';
        for ($i = 0; $i < count($arrCatId); $i++) {
            if ($i == (count($arrCatId) - 1)) {
                $str_cat_id .= $arrCatId[$i];
            } else {
                $str_cat_id .= $arrCatId[$i] . ',';
            }
        }
        if ($str_cat_id == '') {
            $sql = "SELECT id,title,link FROM article WHERE cat_id=" . intval($categoryId) . " AND status='yes' ORDER BY id DESC LIMIT $offset,$limit";
        } else {
            $sql = "SELECT id,title,link FROM article WHERE cat_id=" . intval($categoryId) . " OR cat_id IN ($str_cat_id) AND status='yes' ORDER BY id DESC LIMIT $offset,$limit";
        }

        $rs = $db->query($sql);
        return $rs;
    }

    function ortherArticlesNotInCategory($create_date, $categoryId, $limit=10) {
        global $db, $config;
        if ($create_date != '' AND intval($categoryId) > 0) {
            $limit = ceil($limit / 2);
            $sqlNewer = "SELECT title,link,image,short_description FROM article WHERE create_date > '$create_date' AND cat_id != $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
            $rsNewer = $db->query($sqlNewer);
            $sqlOlder = "SELECT title,link,image,short_description FROM article WHERE create_date < '$create_date' AND cat_id != $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
            $rsOlder = $db->query($sqlOlder);
            return Array(
                "rsNewer" => $rsNewer,
                "rsOlder" => $rsOlder
            );
        }
    }

    function ortherNewerArticleInCategory($create_date, $categoryId, $limit=10) {
        if ($create_date != '' AND intval($categoryId) > 0) {
            global $db, $config;
            $sql = "SELECT title,link,create_date FROM article WHERE create_date > '$create_date' AND cat_id = $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
            $rs = $db->query($sql);
            return $rs;
        }
    }

    function ortherOlderArticleInCategory($create_date, $categoryId, $limit=10) {
        if ($create_date != '' AND intval($categoryId) > 0) {
            global $db, $config;
            $sql = "SELECT title,link,create_date FROM article WHERE create_date < '$create_date' AND cat_id = $categoryId AND status='yes' ORDER BY create_date DESC LIMIT $limit";
            $rs = $db->query($sql);
            return $rs;
        }
    }

    function viewComment($aid) {
        global $db, $config;
        $rs = $db->query("SELECT * FROM comment WHERE 1 AND status='yes' AND aid=" . intval($aid) . " ORDER BY id");
        return $rs;
    }

}

?>
